﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Medianamik.Core.Logging;
using System.Web.Hosting;
using System.Web;
using System.Security.Principal;
using System.Threading;

namespace Medianamik.Core.Sugar
{
    public class Utility
    {
        public static string GetCurrentUserName()
        {
            if (HostingEnvironment.IsHosted)
            {
                HttpContext cur = HttpContext.Current;
                if (cur != null)
                    return cur.User.Identity.Name;
            }
            IPrincipal user = Thread.CurrentPrincipal;
            if (user == null || user.Identity == null)
                return String.Empty;
            else
                return user.Identity.Name;
        }

        public static bool GetFileContentFromPath(string path, out string fileContent)
        {
            fileContent = "";

            if (File.Exists(path))
            {
                StreamReader txtReader = null;

                try
                {
                    txtReader = new StreamReader(path);
                    fileContent = txtReader.ReadToEnd();
                    txtReader.Close();
                    return true;
                }
                catch (Exception)
                { }
                finally
                {
                    if (txtReader != null)
                        txtReader.Close();
                }
            }

            return false;
        }

        public static string FastReplace(string original, string pattern, string replacement, StringComparison comparisonType)
        {
            if (original == null)
                return null;

            if (String.IsNullOrEmpty(pattern))
                return original;

            int lenPattern = pattern.Length;
            int idxPattern = -1;
            int idxLast = 0;

            StringBuilder result = new StringBuilder();

            while (true)
            {
                idxPattern = original.IndexOf(pattern, idxPattern + 1, comparisonType);

                if (idxPattern < 0)
                {
                    result.Append(original, idxLast, original.Length - idxLast);
                    break;
                }

                result.Append(original, idxLast, idxPattern - idxLast);
                result.Append(replacement);

                idxLast = idxPattern + lenPattern;
            }

            return result.ToString();
        }

        /// <summary>  
        /// Gets an enumeration's contents
        /// </summary>  
        /// <typeparam name="TEnum">The type of the enumeration</typeparam>  
        /// <exception cref="ArgumentException">Thrown if the type specified is not an enumeration</exception>  
        /// <returns>A dictionnary with an element's integral value as the key and its name as the value</returns>  
        public static Dictionary<int, string> GetEnumElements<TEnum>()
        {
            var type = typeof(TEnum);
            if (!type.IsEnum)
                throw new ArgumentException("Type must be an enum type");

            var names = Enum.GetNames(type);
            var values = Enum.GetValues(type);

            var elements = new Dictionary<int, string>();
            for (int i = 0; i < names.Length; ++i)
            {
                elements.Add((int)values.GetValue(i), names[i]);
            }
            return elements;
        }

        public static string GetBinPath()
        {
            return Path.GetDirectoryName(new Uri((Assembly
                .GetExecutingAssembly()).CodeBase).LocalPath);
        }
    }
}
